package com.htudata.warning.controller ;

import com.alibaba.fastjson.JSONObject;
import com.htudata.common.enums.RequestSeqEnum;
import com.htudata.common.enums.RequestStatusEnum;
import com.htudata.convention.domain.ResponseResult;
import com.htudata.core.util.ResultGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@Slf4j
@RestController
public class WarningController {

  @Value("${warningSignal.url}")
  private String warningSignalUrl;
  
  @Value("${realTimeWarning.url}")
  private String realTimeWarningUrl;
  
  @GetMapping({"/v1/discrete/warning/wa_fo_inforce//JSON"})
  public ResponseResult getEarlyWarningSignal() {
    try {
      RestTemplate restTemplate = new RestTemplate();
      ResponseEntity<String> responseEntity = restTemplate.getForEntity(this.warningSignalUrl, String.class, new Object[0]);
      String body = (String)responseEntity.getBody();
      return ResultGenerator.genResult(RequestSeqEnum.SEQ_028.getCode(), JSONObject.parseArray(body));
    } catch (Exception e) {
      log.error("getEarlyWarningSignal",e);
      return ResultGenerator.genErrorResult(RequestSeqEnum.SEQ_028.getCode(), RequestStatusEnum.STATUS_503
          .getCode(), "暂无数据");
    } 
  }
  
  @GetMapping({"/v1/discrete/forecast/foreport/{rtype}/{startDateTime}/{endDateTime}/JSON"})
  public ResponseResult getRealTimeWarning(@PathVariable("rtype") String rtype, @PathVariable("startDateTime") String startDateTime, @PathVariable("endDateTime") String endDateTime) {
    try {
      RestTemplate restTemplate = new RestTemplate();
      String url = this.realTimeWarningUrl + rtype + "," + startDateTime + "," + endDateTime + "/JSON";
      ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class, new Object[0]);
      String body = (String)responseEntity.getBody();
      return ResultGenerator.genResult(RequestSeqEnum.SEQ_029.getCode(), JSONObject.parseArray(body));
    } catch (Exception e) {
      log.error("getRealTimeWarning",e);
      return ResultGenerator.genErrorResult(RequestSeqEnum.SEQ_029.getCode(), RequestStatusEnum.STATUS_503
          .getCode(), "暂无数据");
    } 
  }
}
